package com.example.moviecms2.controller;

import com.example.moviecms2.domain.Actor;
import com.example.moviecms2.domain.Movie;
import com.example.moviecms2.domain.Role;
import com.example.moviecms2.repository.ActorRepository;
import com.example.moviecms2.repository.MovieRepository;
import com.example.moviecms2.repository.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.lang.model.element.NestingKind;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private MovieRepository movieRepository;
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private ActorRepository actorRepository;


    @RequestMapping(value = {"/index"})
    public String index(Model model){
        List<Role> list = roleRepository.findAll();
        model.addAttribute("list",list);
        return "role/index";
    }
    @RequestMapping(value = "/add/{movieId}",method = RequestMethod.GET)
    public String toAdd(Model model,@PathVariable Long movieId){
        Movie movie = movieRepository.findOne(movieId);
        model.addAttribute("info",movie);
        //创建演员的list集合
        List<Actor> list  = actorRepository.findAll();
        model.addAttribute("list",list);
        return "role/add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String doAdd(@RequestParam String roleName,@RequestParam Long actorid,@RequestParam Long movieId){
        Movie movie = movieRepository.findOne(movieId);
        Actor actor =actorRepository.findOne(actorid);
        movie.addRole(actor,roleName);
        movieRepository.save(movie);
        return "redirect:/movie/detail/"+movieId;
    }
//通过toEdit()方法跳到角色编辑页面
    @RequestMapping(value = "{id}/edit/{roleId}",method = RequestMethod.GET)
        public String toEdit(Model model,@PathVariable Long id,@PathVariable Long roleId){
        Movie movie = movieRepository.findOne(id);
        Role role = roleRepository.findOne(roleId);

        List<Actor> list  = actorRepository.findAll();
        model.addAttribute("list",list);

        model.addAttribute("info",movie);
        model.addAttribute("roleId",role);
        return "role/edit";

    }
    //通过doEdit方法保存数据重定向回角色编辑页面
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String doEdit(@RequestParam  Long id,
                          @RequestParam  Long roleId,
                          @RequestParam  Long actorid,
                          @RequestParam String roleName){
        Movie movie = movieRepository.findOne(id);
        Role role = roleRepository.findOne(roleId);
        Actor actor = actorRepository.findOne(actorid);

        role.setName(roleName);
        actor.getName();
        role.setActor(actor);
        roleRepository.save(role);
        movieRepository.save(movie);
        return "redirect:/movie/detail/"+movie.getId();
    }


@RequestMapping(value = "{id}/delete/{roleId}",method = RequestMethod.GET)
public String delete(@PathVariable Long id,@PathVariable Long roleId){
        Movie movie = movieRepository.findOne(id);
        roleRepository.delete(roleId);
        return "redirect:/movie/detail/"+movie.getId();
 }
}